#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "interface.h"
#include "debug.h"
#include <pthread.h>

#define buflen 1024*10

char buf[buflen];


int all_len[1000];

void* test_send_fun(void* arg){
	int i;
	int len;

	for(i=0; i<1000; i++){
		debug_info("send data!\n");
		len = rand() % (buflen);
		all_len[i] = len;
		send_data_sync(0,buf, len);
	}

	return NULL;
}

void* test_recv_fun(void* arg){
	msg_t* msg;
	int i=0;
	while(1){
		msg = recv_data(1);
		if(msg->hr.len != all_len[i++]){
				printf("recv len failed! msg->hr.len[%d], all_len[%d], i[%d]\n",
					msg->hr.len, all_len[i], i);
				exit(0);
					 
		}
		free_msg(msg);
	}

	return NULL;
}

 
int main(int arg, char* argv[]){

	int d = 10;
	queue_t q;
	pthread_t pid_send;
	pthread_t pid_recv;
	int rc;
	sys_init(0);

	sleep(10);
  
	rc = pthread_create(&pid_send, NULL, test_send_fun, NULL);
	assert(rc!=-1);
	rc = pthread_create(&pid_recv, NULL, test_recv_fun, NULL);
	assert(rc!=-1);

	pthread_join(pid_send, NULL);
	pthread_join(pid_recv, NULL);
   	 sys_exit();

}
